package com.jt.safe.extension

import androidx.lifecycle.MutableLiveData
import com.jt.safe.base.BaseDataResponse
import com.jt.safe.base.BaseQueryResultResponse
import com.jt.safe.state.ViewState
import com.jt.safe.exception.HubException


/**
 * 处理返回值
 *
 * @param result 请求结果
 */
fun <T> MutableLiveData<ViewState<T>>.paresResult(result: BaseDataResponse<T>) {
    when (result.code) {
        10000 -> this.value = ViewState.onHubSuccess(result.data, result.code)
        1 -> this.value = ViewState.onHubSuccess(result.data, result.code)
        30000 -> this.value = ViewState.onHubSuccess(result.data, result.code)
        else -> this.value = ViewState.onHubError(HubException(result.message), result.code)
    }
}

fun <T> MutableLiveData<ViewState<T>>.paresResult(result: BaseQueryResultResponse<T>) {
    when (result.code) {
        10000 -> this.value = ViewState.onHubSuccess(result.queryResult, result.code)
        else -> this.value = ViewState.onHubError(HubException(result.message), result.code)
    }
}

/**
 * 异常转换异常处理
 *
 * @param e 错误信息
 */
fun <T> MutableLiveData<ViewState<T>>.paresException(e: Throwable) {
    this.value = ViewState.onHubError(HubException(e), 223303)
}